﻿public class CompressRequest
{
    byte[] m_result = null;
    float m_progress = 0f;
    bool m_isDone = false;
    string m_error = null;

    public CompressRequest()
    {

    }

    public void Dispose()
    {

    }

    public bool isDone { get { return m_isDone; } }
    public string error { get { return m_error; } }
    public float progress { get { return m_progress; } }
    public byte[] bytes { get { return m_result; } }

    void OnDone(LoomBase param)
    {
        m_isDone = true;
    }

    public void Compress(byte[] data)
    {
        Loom.RunAsync(new LoomBase(), delegate (LoomBase param)
        {
            try
            {
                m_result = new byte[1];
                int size = 0;//LzmaHelper.Compress(data, ref m_result);
                if (size == 0)
                {
                    m_error = "Compress Failed";
                }
            }
            catch (System.Exception e)
            {
                m_error = e.Message;
            }
            finally
            {
                Loom.QueueOnMainThread(param, OnDone);
            }
        });
    }

    public void Decompress(byte[] data)
    {
        Loom.RunAsync(new LoomBase(), delegate (LoomBase param)
        {
            try
            {
                m_result = new byte[1];
                int size = LzmaHelper.Uncompress(data, ref m_result);
                if (size == 0)
                {
                    m_error = "Compress Failed";
                }
            }
            catch (System.Exception e)
            {
                m_error = e.Message;
            }
            finally
            {
                Loom.QueueOnMainThread(param, OnDone);
            }
        });
    }

    public static CompressRequest CreateCompress(byte[] data)
    {
        CompressRequest request = new CompressRequest();
        request.Compress(data);

        return request;
    }

    public static CompressRequest CreateDecompress(byte[] data)
    {
        CompressRequest request = new CompressRequest();
        request.Decompress(data);

        return request;
    }
}